package ao.erp.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ao.erp.hbm.Syresource;
import ao.erp.hbm.Syrole;
import ao.erp.hbm.SyroleSyreource;
import ao.erp.hbm.Syuser;
import ao.erp.hbm.SyuserSyrole;
import ao.erp.model.DataGrid;
import ao.erp.model.DataGridJson;
import ao.erp.model.Resource;
import ao.erp.service.ResourceServiceI;


@Service("resourceService")
@Transactional
public class ResourceServiceImpl extends BaseServiceImpl implements ResourceServiceI {

	@Override
	public List<Resource> offResourceList() {
		List<Resource> rl = new ArrayList<Resource>();
		List<Syresource> srl = super.getBaseDao().find("from Syresource t where t.onoff = '0'");
		if (srl != null && srl.size() > 0) {
			for (Syresource sr : srl) {
				Resource r = new Resource();
				BeanUtils.copyProperties(sr, r);
				rl.add(r);
			}
		}
		return rl;
	}

	@Override
	public Resource getResourceByRequestPath(String requestPath) {
		Syresource sr = (Syresource) super.getBaseDao().get("from Syresource t where t.url=?", requestPath);
		if (sr != null) {
			Resource r = new Resource();
			BeanUtils.copyProperties(sr, r);
			return r;
		} else {
			return null;
		}
	}

	@Override
	public boolean checkAuth(String userId, String requestPath) {
		boolean b = false;
		Syuser syuser = (Syuser) super.getBaseDao().get(Syuser.class, userId);
		Set<SyuserSyrole> syuserSyroles = syuser.getSyuserSyroles();// 用户和角色关系
		for (SyuserSyrole syuserSyrole : syuserSyroles) {
			Syrole syrole = syuserSyrole.getSyrole();
			Set<SyroleSyreource> syroleSyreources = syrole.getSyroleSyreources();// 角色和资源关系
			for (SyroleSyreource syroleSyreource : syroleSyreources) {
				Syresource syresource = syroleSyreource.getSyresource();
				if (requestPath.equals(syresource.getUrl())) {
					return true;
				}
			}
		}
		return b;
	}

	@Override
	public DataGridJson datagrid(DataGrid dg, Resource resource) {
		DataGridJson j = new DataGridJson();
		String hql = " from Syresource t where 1=1 ";
		String totalHql = " select count(*) " + hql;
		j.setTotal(super.getBaseDao().count(totalHql));// 设置总记录数
		if (dg.getSort() != null) {// 设置排序
			hql += " order by " + dg.getSort() + " " + dg.getOrder();
		}
		List<Syresource> ol = super.getBaseDao().find(hql);// 查询分页
		List<Resource> nl = new ArrayList<Resource>();
		if (ol != null && ol.size() > 0) {// 转换模型
			for (Syresource ot : ol) {
				Resource nt = new Resource();
				BeanUtils.copyProperties(ot, nt);
				if (ot.getSyresource() != null) {
					nt.set_parentId(ot.getSyresource().getId());
				}
				nl.add(nt);
			}
		}
		j.setRows(nl);// 设置返回的行
		return j;
	}

	@Override
	public void delete(String id) {
		Syresource syresource = (Syresource) super.getBaseDao().load(Syresource.class, id);
		Set<SyroleSyreource> syroleSyreources = syresource.getSyroleSyreources();
		for (SyroleSyreource syroleSyreource : syroleSyreources) {
			super.getBaseDao().delete(syroleSyreource);
		}
		super.getBaseDao().delete(syresource);
	}
}
